package com.ruikc.study.demo.util;

import java.util.HashMap;
import java.util.Map;

class ResultCode {
    static final Integer SUCCESS = 200;
    static final Integer ERROR = 201;
    static final Integer NO_AUTH = 401;
    static final Integer DENIED = 403;
}


public class Result {
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    // 把构造方法私有
    private Result() {}

    // 成功静态方法
    public static Result ok() {
        Result r = new Result();
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("操作成功");
        return r;
    }

    public static Result ok(String message) {
        Result r = new Result();
        r.setCode(ResultCode.SUCCESS);
        r.setMessage(message);
        return r;
    }

    // 失败静态方法
    public static Result error() {
        Result r = new Result();
        r.setCode(ResultCode.ERROR);
        r.setMessage("操作失败");
        return r;
    }

    public static Result error(String message) {
        Result r = new Result();
        r.setCode(ResultCode.ERROR);
        r.setMessage(message);
        return r;
    }

    public static Result no_auth() {
        Result r = new Result();
        r.setCode(ResultCode.NO_AUTH);
        r.setMessage("请先登录");
        return r;
    }

    public static Result deniy() {
        Result r = new Result();
        r.setCode(ResultCode.DENIED);
        r.setMessage("无权限访问");
        return r;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(String key, Object value){
        
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
